package cn.tedu.movie.controller;

import cn.tedu.movie.entity.Movie;
import cn.tedu.movie.entity.MovieVo;
import cn.tedu.movie.entity.SessionVo;
import cn.tedu.movie.mapper.MovieMapper;
import cn.tedu.movie.mapper.SessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/session/")
public class SessionController {
    @Autowired(required = false)
    SessionMapper sessionMapper;
    @Autowired(required = false)
    MovieMapper movieMapper;

    @GetMapping("layout")
    String layout(Integer id) {
        return sessionMapper.layout(id);
    }

    @RequestMapping("price")
    Integer price(Integer id) {
        return sessionMapper.price(id);
    }

    //-------------------后端-------------------------------
    @GetMapping("sessionlist")
    List<SessionVo> movieList(Integer page, Integer limit) throws ParseException {
        List<SessionVo> list = sessionMapper.sessionlist((page - 1) * limit, limit);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (SessionVo sessionVo : list) {
            String date = sdf.format(sessionVo.getStartDate());
            sessionVo.setStart(date);
        }
        if (list.size() > 0)
            list.get(0).setCount(sessionMapper.sessionCount());
        return list;
    }

    @PostMapping("insertSession")
    int insertMovie(SessionVo sessionVo) throws IOException, ParseException {
        int num = sessionMapper.check(sessionVo.getHallId(), sessionVo.getSessId()
                , sessionVo.getDateId());
        if (num!=0) {
            return 2;//当前场次已被使用
        }
        String date="";
        Integer dateId = sessionVo.getDateId();
        switch (dateId) {
            case 1:
                date = "2021-12-01 ";
                break;
            case 2:
                date = "2021-12-02 ";
                break;
            case 3:
                date = "2021-12-03 ";
                break;
        }
        Integer sessId = sessionVo.getSessId();
        String time="";
        switch (sessId) {
            case 1:
                time = "12:00";
                break;
            case 2:
                time = "15:00";
                break;
            case 3:
                time = "18:00";
                break;
            case 4:
                time = "21:00";
                break;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d=sdf.parse(date+time);
        sessionVo.setStartDate(d);
        Integer num2 = sessionMapper.insertSession(sessionVo);
        return num2;
    }
    @GetMapping("deleteById")
    public void deleteById(Integer id){
        sessionMapper.deleteById(id);
    }
    @PostMapping("deleteByIds")
    public void deleteByIds(String ids) {
        System.out.println(ids);
        String[] strs = ids.split("[\\[\\],]");
        for (String str : strs) {
            if (!str.isEmpty()){
                Integer id=Integer.parseInt(str);
                 sessionMapper.deleteById(id);
            }
        }
    }
    @GetMapping("backstageSelectOne")
    String backstageSelect(Integer id){
        return sessionMapper.backstageSelect(id);
    }
    @PostMapping("updateSession")
    int updateSession(Integer movieId,Integer id){
        return sessionMapper.updateSession(movieId,id);
    }
}
